package com.wildwolf.project.wildwolf.ui.addressbook;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ExpandableListView;
import android.widget.TextView;

import com.wildwolf.project.wildwolf.R;
import com.wildwolf.project.wildwolf.adapter.AddressBookExpandableListAdapter;
import com.wildwolf.project.wildwolf.config.AppConfig;
import com.wildwolf.project.wildwolf.model.CustomizationContact;

public class AddressBookActivity extends ListActivity implements
		OnClickListener {
	private TextView returnTextView = null;

	private static final Intent SERVICE_INTENT = new Intent();
	static {
		SERVICE_INTENT.setComponent(new ComponentName(
				"com.wildwolf.project.wildwolf",
				"com.wildwolf.project.wildwolf.WildWolfService"));
	}

	private ExpandableListView expandableListView = null;
	private AddressBookExpandableListAdapter adapter = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_addressbook);
		expandableListView = (ExpandableListView) findViewById(android.R.id.list);
		returnTextView = (TextView) findViewById(R.id.addressbook_textView_return);
		returnTextView.setOnClickListener(this);
	}

	@Override
	protected void onStart() {
		super.onStart();
		List<String> addressBookGroup;
		List<List<CustomizationContact>> addressBookChild;
		addressBookGroup = new ArrayList<String>();
		addressBookGroup.add(AppConfig.AdressBookKey.ENCOUNTER_GROUP_PREFIX);
		addressBookGroup.add(AppConfig.AdressBookKey.PHONE_GROUP_PREFIX);
		addressBookChild = new ArrayList<List<CustomizationContact>>();
		List<CustomizationContact> encounterChild = new ArrayList<CustomizationContact>();
		List<CustomizationContact> phoneChild = new ArrayList<CustomizationContact>();
		CustomizationContact contact = null;
		for (int i = 0; i < 5; i++) {
			contact = new CustomizationContact("偶遇--张" + i, "18683449527",
					"不以结婚为目的的谈恋爱的都是耍流氓");
			encounterChild.add(contact);
		}
		ContentResolver contentResolver = getContentResolver();
		Cursor cursor = contentResolver.query(
				ContactsContract.Data.CONTENT_URI, null, null, null, null);
		startManagingCursor(cursor);
		cursor.moveToFirst();
		while (!cursor.isLast()) {
			contact = new CustomizationContact(
					cursor.getString(cursor
							.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)),
					"",
					cursor.getString(cursor
							.getColumnIndex(ContactsContract.Contacts.Data.DATA1)));
			phoneChild.add(contact);
			cursor.moveToNext();
		}
		addressBookChild.add(encounterChild);
		addressBookChild.add(phoneChild);
		adapter = new AddressBookExpandableListAdapter(addressBookGroup,
				addressBookChild, this);
		expandableListView.setAdapter(adapter);
	}

	@Override
	public void onClick(View view) {
		if (returnTextView == view) {

		}
	}

}
